<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="字典种类:" prop="typeName">
        <el-input v-model="ruleForm.typeName" style="width:500px;"></el-input>
      </el-form-item>
      <el-form-item label="字典描述:" prop="dicDesc">
        <el-input v-model="ruleForm.dicDesc" style="width:500px;"></el-input>
      </el-form-item>
      <el-form-item label="key值:" prop="dicKey">
        <el-input v-model="ruleForm.dicKey" style="width:500px;"></el-input>
      </el-form-item>
      <el-form-item label="value值:" prop="dicValue">
        <el-input v-model="ruleForm.dicValue" style="width:500px;"></el-input>
      </el-form-item>
      <el-form-item label="字典排序:" prop="dicOrder">
        <el-input v-model="ruleForm.dicOrder" style="width:500px;"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="primary" @click="backForm">返回</el-button>
      </el-form-item>
    </el-form>
    <!-- <el-button :plain="true" @click="open2">成功</el-button> -->
  </div>
</template>
<script>
export default {
  name: "shareAdd",
  data() {
    return {
      pageType: 1,
      ruleForm: {
        typeName: "",
        dicDesc: "",
        dicKey: "",
        dicValue: "",
        dicOrder: ""
      },
      rules: {
        typeName: [
          { required: true, message: "请输入字典种类", trigger: "blur" }
        ],
        dicDesc: [
          { required: true, message: "请输入字典描述", trigger: "blur" }
        ],
        dicKey: [{ required: true, message: "请输入key值", trigger: "blur" }],
        dicValue: [
          { required: true, message: "请输入value值", trigger: "blur" }
        ],
        dicOrder: [
          { required: true, message: "请输入字典排序", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.pageType = this.$route.query.pageType;
    if (this.pageType == 2) {
      this.ruleForm = this.$route.query.row;
    }
  },

  methods: {
    //   提交
    submitForm(formName) {
      console.log(formName);
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.ruleForm);
          if (this.pageType == 1) {
            this.$axios
              .post("/city/sysdic/dicAdd", this.ruleForm)
              .then(res => {
                this.$message({
                  message: "添加成功",
                  type: "success"
                });
                this.$router.push("/share");
              })
              .catch(error => {});
          } else {
            this.$axios
              .post("/city/sysdic/dicUpdate", this.ruleForm)
              .then(res => {
                console.log(res);
                this.$message({
                  message: "编辑成功",
                  type: "success"
                });
                this.$router.push("/share");
              })
              .catch(error => {});
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 返回
    backForm() {
      this.$router.push("/share");
    }
  }
};
</script>